package com.hong.strategy.rpcserver;

import com.hong.Config.ConfigManager;
import com.hong.RpcServer;
import com.hong.config.constant.ServerConfig;
import com.hong.handler.HttpRequestHandler;
import com.hong.listener.impl.RpcServerConfigurer;
import com.hong.qrpc.server.HttpTransportServer;
import com.hong.qrpc.utils.ReflectionUtils;
import com.hong.qrpc.utils.StringUtils;
import com.hong.strategy.PropertiesStrategy;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @Author HQT
 * @Date 2022/7/17 14:36
 */
@Slf4j
public class TransportClassStrategy implements PropertiesStrategy {
    @Override
    public void disposeProperties(RpcServer rpcServer) {
        ConfigManager configManager = rpcServer.getConfigManager();
        Map<Object, Object> profile = configManager.getProfile();
        String manner = (String) profile.get(ServerConfig.QRPC_TRANSPORT_MANNER);
        if (!StringUtils.isEmpty(manner)) {
            switch (manner.toLowerCase()) {
                case "http":
                    configManager.getRpcServerConfig().setTransportClass(HttpTransportServer.class);
                    rpcServer.setNet(ReflectionUtils.newInstance(configManager.getRpcServerConfig().getTransportClass()));
                    rpcServer.setHandler(new HttpRequestHandler(rpcServer));
                    RpcServerConfigurer.addListener(rpcServer.getListenerManager());
                    return;
                case "netty":
                    //use default configuration
                    return;
                default:
                    log.warn("unsupported transporting manner {},use transporting manner \"netty\" now", manner);
            }
        }
    }
}
